/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.provider.why10w.persistent;

import cn.hutool.core.io.FileUtil;
import com.example.spider.provider.why10w.Why10wFileNameTemplate;
import com.example.spider.provider.why10w.dto.WhyTaskResult;
import lombok.extern.slf4j.Slf4j;
import org.shoulder.core.context.AppInfo;
import org.shoulder.core.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author lym
 */
@Slf4j
@Component
public class Why10TaskExporter {

    @Autowired
    private Why10wFileNameTemplate why10wFileNameTemplate;

    public void output(WhyTaskResult result, String html) {
        // 保存到文件
        String fileName = why10wFileNameTemplate.calculateParsedResultFileName(result.getSource(), result.getId());
        FileUtil.writeString(JsonUtils.toJson(result), fileName, AppInfo.charset());

        String htmlFileName = why10wFileNameTemplate.calculateHtmlFileName(result.getSource(), result.getId());
        FileUtil.writeString(html, htmlFileName, AppInfo.charset());

        log.info("save {} finished jsonFile={}, html={}", result.getUrl(), fileName, htmlFileName);
    }
}